<?php 
namespace app\api\controller\v1;
use think\Db;
// 统计
class Index extends Base {
    
    // 获取统计
    public function count() {
       $alliance = Db::table('hwl_alliance')->where('status', 4)->count();
       $supplier = Db::table('hwl_supplier')->count();
       $warehouse = Db::table('hwl_warehouse')->count();
       $product = Db::table('hwl_product')->count();
       $finished = Db::table('hwl_finished')->count();
       $order = Db::table('hwl_order')
                ->where('type', 1)
                ->where('status', 1)
                ->count();
       
       return show(1, '成功', [
           'alliance' => $alliance,
           'supplier' => $supplier,
           'warehouse' => $warehouse,
           'product' => $product,
           'finished' => $finished,
           'order' => $order
       ], 200);
    }
    // 销售采购占比
    public function pie() {
        // 销售 价格
        $xs1 = Db::table('hwl_stock')
        ->where('status', 1)
        ->where('type', 1)
        ->sum('total_price');

        $xs2 = Db::table('hwl_stock')
        ->where('status', 1)
        ->where('type', 2)
        ->sum('total_price');

        $stock = ($xs1 - $xs2);

        // 采购 价格
        $cg1 = Db::table('hwl_order')
        ->where('status', 1)
        ->where('type', 1)
        ->sum('total_price');

        $cg2 = Db::table('hwl_order')
        ->where('status', 1)
        ->where('type', 2)
        ->sum('total_price'); 

        $order = ($cg1 - $cg2);

        return show(1, '成功', [
            'stock' => $stock,
            'order' => $order
        ], 200);
    }
    // 获取一周销售
    public function chartsDay() {
        $time['end_time'] = date('Y-m-d', time());
        $time['start_time'] = date('Y-m-d', strtotime("-6 day"));

        $time_sc = dayTime($time);
        $date_json = json_encode($time_sc);

        $arr = [];
        foreach($time_sc as $key => $value) {
            $start = $value . ' 00:00:00';
            $end = $value . ' 24:00:00';
            $tempV = $data = Db::table('hwl_stock')
            ->where('create_time', 'between time', [strtotime($start), strtotime($end)])
            ->sum('total_price');
            $arr[$value] = $tempV;
        }

        return show(1, '成功', $arr, 200);
    }
}